package org.argeproje.resim.proc.view;
import javax.media.jai.InterpolationNearest;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;

import java.awt.*;
import java.awt.image.DataBuffer;
import java.awt.image.renderable.ParameterBlock;

import javax.swing.*;

import org.argeproje.resim.ResimProjectEditor;
import org.argeproje.resim.ViewerPage;
import org.argeproje.resim.proc.ConnectionRule;
import org.argeproje.resim.proc.data.Data;
import org.argeproje.resim.proc.data.ImageDA;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;



import com.sun.media.jai.widget.DisplayJAI;



public class ImgViewerPR extends ViewerPR {
	protected PlanarImage _originalImage;
	protected PlanarImage _formattedImage;	
	protected float _currentZoomX = 1.0F;
	protected float _currentZoomY = 1.0F;
	protected void setConnectionRules(ConnectionRule connRule){
		connRule.addInType(ImageDA.class, 0);
	}
	public Data process() {
		_originalImage = getInputAsPlanarImage();
		setOriginalImage(_originalImage);
		ParameterBlock pbConvert = new ParameterBlock();
		pbConvert.addSource(_originalImage);
		pbConvert.add(DataBuffer.TYPE_BYTE);
		_formattedImage = JAI.create("format", pbConvert);
        setFormattedImage(_formattedImage);
		JPanel panel = new DisplayJAI(_formattedImage);
		panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
	    setViewPanel(new DisplayJAI(_formattedImage),"id","ImageViewer");
	    return null;
	}
	protected void zoom(float x, float y){	
		ParameterBlock pb = new ParameterBlock();
		pb.addSource(getFormattedImage());                    // The source image
		pb.add(x);                         // The xScale
		pb.add(y);                         // The yScale
		pb.add(0.0F);                        // The x translation
		pb.add(0.0F);                        // The y translation
		pb.add(new InterpolationNearest()); // The interpolation
		// Create the scale operation
		PlanarImage im = JAI.create("scale", pb, null);
        DisplayJAI panel = (DisplayJAI)getViewPanel(0);
        panel.set(im);	 
	}
	protected void translateOrigin(int x, int y){
        DisplayJAI panel = (DisplayJAI)getViewPanel(0);
        panel.setOrigin((int)panel.getOrigin().getX() + x, (int)panel.getOrigin().getY() + y); 
		return;
	}
	protected PlanarImage getOriginalImage(){
		return _originalImage;
	}
	protected void setOriginalImage(PlanarImage image){
		_originalImage = image;
	}
	protected PlanarImage getFormattedImage(){
		return _formattedImage;
	}
	protected void setFormattedImage(PlanarImage image){
		_formattedImage = image;
	}
	public void zoomOut(){
		float zoomFactor = 1 / ((float)1.0 + 0.2F);
		_currentZoomX *= zoomFactor;
		_currentZoomY *= zoomFactor;
		zoom(_currentZoomX, _currentZoomY);
	}
	public void zoomIn(){
		float zoomFactor = (float)1.0 + 0.2F;
		_currentZoomX *= zoomFactor;
		_currentZoomY *= zoomFactor;
		zoom(_currentZoomX, _currentZoomY);
	}
	public void save(String fileName){
		ParameterBlock pb = new ParameterBlock();
		pb.addSource(getFormattedImage());          
		pb.add(fileName);                     
		pb.add("JPEG");                        
		JAI.create("filestore", pb, null);		
	}
}